package com.wanbatv.wangwangba;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.VideoView;

import com.wanbatv.wangwangba.util.Util;

/**
 * Created by Forcs on 15/10/30.
 */
public class VideoPlayerActivity extends FullScreenActivity {

    private VideoView mVideoView = null;
    private String mUrl = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_player);
        mVideoView = (VideoView) findViewById(R.id.video_view);

        Intent intent = getIntent();
        mUrl = intent.getStringExtra("url");
        mVideoView.setVideoPath("http://cdn.wanbatv.com" + mUrl + Util.encryptUrl(mUrl));
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Intent data = new Intent();
                data.putExtra("key_code", KeyEvent.KEYCODE_BACK);
                setResult(RESULT_OK, data);
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mVideoView.start();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mVideoView.stopPlayback();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_LEFT:
                Intent data = new Intent();
                data.putExtra("key_code", keyCode);
                setResult(RESULT_OK, data);
                finish();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
